package com.pdd.mall.controller;

import com.pdd.mall.common.api.CommonResult;
import com.pdd.mall.entity.Category;
import com.pdd.mall.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 分类控制器
 */
@RestController
@RequestMapping("/categories")
public class CategoryController {
    
    @Autowired
    private CategoryService categoryService;
    
    @GetMapping
    public CommonResult<List<Category>> getAllCategories() {
        List<Category> categoryList = categoryService.getAllCategories();
        return CommonResult.success(categoryList);
    }
    
    @GetMapping("/{id}")
    public CommonResult<Category> getCategoryById(@PathVariable Long id) {
        Category category = categoryService.getCategoryById(id);
        if (category == null) {
            return CommonResult.notFound("分类不存在");
        }
        return CommonResult.success(category);
    }
    
    @GetMapping("/parent/{parentId}")
    public CommonResult<List<Category>> getCategoriesByParentId(@PathVariable Long parentId) {
        List<Category> categoryList = categoryService.getCategoriesByParentId(parentId);
        return CommonResult.success(categoryList);
    }
}
